import { safeRemoveFromFather } from "../ui/uiUtils";

export class MenuItem extends eui.Component {
	constructor() {
		super();
		this.skinName = 'menuItemSkin';
	}

	labelDisplay: eui.Label;
}

export class ContexMenu extends eui.Component {
	constructor() {
		super();
		this.skinName = 'menuSkin';
		this.touchChildren = true;
	}

	group: eui.Group;

	showContexMenu(
		menus: string[],
		callback: (menu:string)=>void,
		self: egret.DisplayObject,
		_width: number,
	): void {
		const width = _width || this.width;
		this.width = width;
		for (let i = 0; i < menus.length; i++) {
			// let btn = new eui.Button()
			const btn = new MenuItem();
			btn.width = width;
			btn.labelDisplay.text = menus[i];
			// btn["labelDisplay"].text = menus[i]
			btn.addEventListener(
				'touchTap',
					// eslint-disable-next-line no-loop-func
					(): void => {
					callback.apply(self, [menus[i]]);
					safeRemoveFromFather(this);
				},
				null,
			);
			this.group.addChild(btn);
		}
	}

	getMenuHeight(): number {
		return this.group.numChildren * 50;
	}
}
